﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using NetworkManagement;

namespace LorraineSec.MessageLib.Messages
{
    public sealed class EditAssetMessage : EncryptedMessage
    {
        #region Format Constants
        /*
         * Format:
         *     4          4             1        4          1            x
         * |AssetID|CompartmentID|AssetStatus|UserID|AssetNameLength|AssetName|
         */

        //Lengths
        private const int ENC_ASSETID_LENGTH = 4;
        private const int ENC_COMPARTMENTID_LENGTH = 4;
        private const int ENC_ASSETSTATUS_LENGTH = 1; //Zach: not sure if this is supposed to be one byte
        private const int ENC_USERID_LENGTH = 4;
        private const int ENC_ASSETNAMELENGTH_LENGTH = 1;

        //Offsets
        private const int ENC_ASSETID_OFFSET = 0;
        private const int ENC_COMPARTMENTID_OFFSET = ENC_ASSETID_OFFSET + ENC_ASSETID_LENGTH;
        private const int ENC_ASSETSTATUS_OFFSET = ENC_COMPARTMENTID_OFFSET + ENC_COMPARTMENTID_LENGTH;
        private const int ENC_USERID_OFFSET = ENC_ASSETSTATUS_OFFSET + ENC_ASSETSTATUS_LENGTH;
        private const int ENC_ASSETNAMELENGTH_OFFSET = ENC_USERID_OFFSET + ENC_USERID_LENGTH;
        private const int ENC_ASSETNAME_OFFSET = ENC_ASSETNAMELENGTH_OFFSET + ENC_ASSETNAMELENGTH_LENGTH;

        //Known Length
        private const int ENC_KNOWN_LENGTH = ENC_ASSETID_LENGTH + ENC_COMPARTMENTID_LENGTH + ENC_ASSETSTATUS_LENGTH + ENC_USERID_LENGTH + ENC_ASSETNAMELENGTH_LENGTH;
        
        #endregion

        public uint AssetID;
        public uint CompartmentID;
        public Status AssetStatus;
        public uint UserID;
        public string AssetName;

        public enum Status : byte
        {
            UNKNOWN,
            IN,
            OUT,
        }

        public EditAssetMessage()
        {
            init();
        }

        public EditAssetMessage(byte[] bytes)
            : base(bytes)
        {
            init();
            decode();
        }

        private void init()
        {
            MessageID = (ushort)LorraineSecMessageFactory.MessageID.EDIT_ASSET;
        }

        public override void decrypt(CryptoManager crypt)
        {
            //Decrypt byte array
            base.decrypt(crypt);
            //Decode byte array
            AssetID = Bytes.getUInt32(encData, ENC_ASSETID_OFFSET);
            CompartmentID = Bytes.getUInt32(encData, ENC_COMPARTMENTID_OFFSET);
            AssetStatus = (Status) Bytes.getByte(encData, ENC_ASSETSTATUS_OFFSET); //Zach: Not sure if this is the right function
            UserID = Bytes.getUInt32(encData, ENC_USERID_OFFSET);
            AssetName = Bytes.getString(encData, ENC_ASSETNAME_OFFSET, Bytes.getByte(encData, ENC_ASSETNAMELENGTH_OFFSET));
        }

        public override void encrypt(CryptoManager crypt)
        {
            //Encode Byte Array
            encData = new byte[ENC_KNOWN_LENGTH + AssetName.Length];
            Bytes.setValue(encData, ENC_ASSETID_OFFSET, AssetID);
            Bytes.setValue(encData, ENC_COMPARTMENTID_OFFSET, CompartmentID);
            Bytes.setValue(encData, ENC_ASSETSTATUS_OFFSET, (byte) AssetStatus);
            Bytes.setValue(encData, ENC_USERID_OFFSET, UserID);
            Bytes.setValue(encData, ENC_ASSETNAMELENGTH_OFFSET, (byte) AssetName.Length);
            Bytes.setValue(encData, ENC_ASSETNAME_OFFSET + AssetName.Length, AssetName);
            //Encrypt Byte Array
            base.encrypt(crypt);
        }
        
    }
}
